package MainPage;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * listAllEvent.java
 *
 * Created on 28 Oktober 2010, 3:15:24 AM
 */

import java.awt.Color;
import java.awt.Cursor;

import javax.swing.ButtonGroup;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
/**
 *
 * @author EK
 */
public class listAllEvent extends javax.swing.JFrame {

    //"jdbc:oracle:thin:@localhost:1521:XE","java","java"
    /** Creates new form listAllEvent */
    public listAllEvent() {
        initComponents();
        init();
        initGetTable();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setSize(1200,725);
        setLocationRelativeTo(null);
    }
    

    
    private void updateRow(String edit)
    {
        try
        {
            userMainPage.stmt.executeUpdate(edit);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(listAllEvent.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        setDateBtn = new javax.swing.JButton();
        sortBtn1 = new javax.swing.JButton();
        sortBtn2 = new javax.swing.JButton();
        lblSort = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        deleteBtn = new javax.swing.JButton();
        listAllBtn = new javax.swing.JButton();
        backBtn = new javax.swing.JLabel();
        shaolinBtn = new javax.swing.JLabel();
        Background = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LIST ALL EVENT");

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setToolTipText("Click On Header to Sort");
        jTable1.setDragEnabled(true);
        jTable1.setFillsViewportHeight(true);
        jTable1.setSelectionBackground(new java.awt.Color(255, 129, 25));
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jTable1);

        jScrollPane1.setBounds(80, 140, 710, 420);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        setDateBtn.setText("CLICK HERE TO SET DATE");
        setDateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setDateBtnActionPerformed(evt);
            }
        });
        setDateBtn.setBounds(400, 40, 240, 23);
        jLayeredPane1.add(setDateBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        sortBtn1.setText("Sort");
        sortBtn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortBtn1ActionPerformed(evt);
            }
        });
        sortBtn1.setBounds(650, 40, 70, 23);
        jLayeredPane1.add(sortBtn1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        sortBtn2.setText("Sort");
        sortBtn2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortBtn2ActionPerformed(evt);
            }
        });
        sortBtn2.setBounds(650, 80, 70, 23);
        jLayeredPane1.add(sortBtn2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lblSort.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblSort.setText("Filter By :");
        lblSort.setBounds(120, 40, 80, 20);
        jLayeredPane1.add(lblSort, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Birthday", "Sport", "Meeting", "Travelling", "Party", "Other" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jComboBox1.setBounds(400, 80, 220, 20);
        jLayeredPane1.add(jComboBox1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jRadioButton2.setBackground(new java.awt.Color(255, 169, 25));
        jRadioButton2.setText("EVENT TYPE");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });
        jRadioButton2.setBounds(240, 80, 130, 23);
        jLayeredPane1.add(jRadioButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jRadioButton1.setBackground(new java.awt.Color(255, 169, 25));
        jRadioButton1.setText("DATE");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });
        jRadioButton1.setBounds(240, 40, 130, 23);
        jLayeredPane1.add(jRadioButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        deleteBtn.setBackground(new java.awt.Color(255, 153, 0));
        deleteBtn.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        deleteBtn.setText("DELETE");
        deleteBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBtnActionPerformed(evt);
            }
        });
        deleteBtn.setBounds(480, 580, 90, 30);
        jLayeredPane1.add(deleteBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        listAllBtn.setBackground(new java.awt.Color(255, 153, 0));
        listAllBtn.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        listAllBtn.setText("LIST ALL");
        listAllBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listAllBtnActionPerformed(evt);
            }
        });
        listAllBtn.setBounds(340, 580, 90, 30);
        jLayeredPane1.add(listAllBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        backBtn.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        backBtn.setForeground(new java.awt.Color(255, 255, 255));
        backBtn.setText("BACK");
        backBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        backBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                backBtnMouseEntered(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                backBtnMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                backBtnMouseExited(evt);
            }
        });
        backBtn.setBounds(1090, 630, 60, 30);
        jLayeredPane1.add(backBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        shaolinBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/shaolinBtnEdit.png"))); // NOI18N
        shaolinBtn.setText(" ");
        shaolinBtn.setBounds(866, 329, 334, 371);
        jLayeredPane1.add(shaolinBtn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Background.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/shaolinEdit.png"))); // NOI18N
        Background.setText(" ");
        Background.setBounds(0, 0, 1200, 700);
        jLayeredPane1.add(Background, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    
    public void check()
	{
		checkUpcomingEvent check_page=new checkUpcomingEvent()
		{
			public String getParameter(String name)
			{
		           if (name.equalsIgnoreCase("msg"))
		           {
		              return "Local Frame";
		           }
		           return null;
		    }
		};
		//calender_page.setSize(userMainPage.screensize.width,userMainPage.screensize.height);
		this.dispose();
		check_page.setVisible(true);
	}
    private void backBtnMouseClicked(java.awt.event.MouseEvent evt) {
    	firstPage.playAudio2(getClass().getResource("/resource/gong-chinese.wav").getFile());
    	check();
    }
    

    private void backBtnMouseEntered(java.awt.event.MouseEvent evt) {
        shaolinBtn.setVisible(true);
        backBtn.setForeground(Color.yellow);
    }

    private void backBtnMouseExited(java.awt.event.MouseEvent evt) {
        shaolinBtn.setVisible(false);
        backBtn.setForeground(Color.white);
    }

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jComboBox1.setEnabled(false);
        sortBtn2.setEnabled(false);
        setDateBtn.setEnabled(true);
        sortBtn1.setEnabled(true);
    }

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        setDateBtn.setEnabled(false);
        sortBtn1.setEnabled(false);
        jComboBox1.setEnabled(true);
        sortBtn2.setEnabled(true);
    }

    public void myCalendar()
	{
		calendarGUI calender_page=new calendarGUI()
		{
			public String getParameter(String name)
			{
		           if (name.equalsIgnoreCase("msg"))
		           {
		              return "Local Frame";
		           }
		           return null;
		    }
		};
		//calender_page.setSize(userMainPage.screensize.width,userMainPage.screensize.height);
		calendarGUI.jButton1.setText("OK");
		calender_page.setVisible(true);
	}
    
    private void setDateBtnActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	
    	myCalendar();
    	
    }

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }
    
    private DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();

    class CenterRenderer extends DefaultTableCellRenderer
    {
	public CenterRenderer()
	{
		setHorizontalAlignment( CENTER );
	}
    }
    
    private void sortBtn2ActionPerformed(java.awt.event.ActionEvent evt) {
    		CenterRenderer centerRenderer = new CenterRenderer();
            NewTableModel newTable2 = new NewTableModel();
            String choice = jComboBox1.getSelectedItem().toString();
            int countRow = 0;
            int countColumn = 0;
            int countRowControl = 0;
            int countColumnControl = 0;
        
       try {

           //EVENT_DATE,EVENT_TIME,EVENT_TYPE,EVENT_TITLE,EVENT_DES
            String str = "SELECT EVENT_DATE,EVENT_TIME,EVENT_TYPE,EVENT_TITLE,EVENT_DES FROM EVENT WHERE EVENT_TYPE='" + choice + "'AND USER_CODE="+userMainPage.userCode+" ORDER BY EVENT_DATE";
            ResultSet rset = userMainPage.stmt.executeQuery(str);
            
           
            //creating 2d arrays of appropriate size according to database
            while (rset.next()){
                countRow++;
            }
        
            ResultSetMetaData metadata = rset.getMetaData();
            countColumn = metadata.getColumnCount();

            newTable2.setDataSize(countRow, 5);
            newTable2.setColumnNameSize();
            //end

            //adding column names to table model
            DatabaseMetaData meta = userMainPage.con.getMetaData();

            ResultSet rset2 = meta.getColumns(null, null, "TYPE", null);

            
            newTable2.setColumnName(0, "DATE");
            newTable2.setColumnName(1, "TIME");
            newTable2.setColumnName(2, "TYPE");
            newTable2.setColumnName(3, "EVENT");
            newTable2.setColumnName(4, "DESCRIPTION");
           
            rset = userMainPage.stmt.executeQuery(str);
            while(rset.next()){
                
            newTable2.addString(countRowControl, countColumnControl, rset.getString("EVENT_DATE"));
            countColumnControl++;
                        
            newTable2.addString(countRowControl, countColumnControl, rset.getString("EVENT_TIME"));
            countColumnControl++;
            
            newTable2.addString(countRowControl, countColumnControl, rset.getString("EVENT_TYPE"));
            countColumnControl++;
        
            newTable2.addString(countRowControl, countColumnControl, rset.getString("EVENT_TITLE"));
            countColumnControl++;
       
            newTable2.addString(countRowControl, countColumnControl, rset.getString("EVENT_DES"));
            countColumnControl++;
        
            countColumnControl = 0;
            countRowControl++;
                
            }
             }

        catch (Exception e){
            System.out.println("error");
        }
             
             
        jTable1.setModel(newTable2);
        jTable1.show();
        for(int i = 0; i < 4; i++){
            TableColumn column = jTable1.getColumnModel().getColumn(i);
            if (i==0) {
                column.setCellRenderer( centerRenderer );
            }
            if (i==1) {
                column.setCellRenderer( centerRenderer );
            }
            if (i==2) {
                column.setCellRenderer( centerRenderer );
            }
            if (i==3) {
                column.setPreferredWidth(40);
            }
    }
    }

    private void sortBtn1ActionPerformed(java.awt.event.ActionEvent evt) {
    		CenterRenderer centerRenderer = new CenterRenderer();
            NewTableModel newTable2 = new NewTableModel();
            String choice = jComboBox1.getSelectedItem().toString();
            int countRow = 0;
            int countColumn = 0;
            int countRowControl = 0;
            int countColumnControl = 0;

       try {

            String str = "SELECT EVENT_DATE,EVENT_TIME,EVENT_TYPE,EVENT_TITLE,EVENT_DES FROM EVENT WHERE EVENT_DATE='"+setDateBtn.getText()+"'AND USER_CODE="+userMainPage.userCode+" ORDER BY EVENT_TIME";
            ResultSet rset = userMainPage.stmt.executeQuery(str);


            //creating 2d arrays of appropriate size according to database
            while (rset.next()){
                countRow++;
            }

            ResultSetMetaData metadata = rset.getMetaData();
            countColumn = metadata.getColumnCount();

            newTable2.setDataSize(countRow, 5);
            newTable2.setColumnNameSize();
            //end

            //adding column names to table model
            DatabaseMetaData meta = userMainPage.con.getMetaData();

            ResultSet rset2 = meta.getColumns(null, null, "TYPE", null);


            newTable2.setColumnName(0, "DATE");
            newTable2.setColumnName(1, "TIME");
            newTable2.setColumnName(2, "TYPE");
            newTable2.setColumnName(3, "EVENT");
            newTable2.setColumnName(4, "DESCRIPTION");

            rset = userMainPage.stmt.executeQuery(str);
            while(rset.next()){

            newTable2.addString(countRowControl, countColumnControl, rset.getString("EVENT_DATE"));
            countColumnControl++;

            newTable2.addString(countRowControl, countColumnControl, rset.getString("EVENT_TIME"));
            countColumnControl++;

            newTable2.addString(countRowControl, countColumnControl, rset.getString("EVENT_TYPE"));
            countColumnControl++;

            newTable2.addString(countRowControl, countColumnControl, rset.getString("EVENT_TITLE"));
            countColumnControl++;

            newTable2.addString(countRowControl, countColumnControl, rset.getString("EVENT_DES"));
            countColumnControl++;

            countColumnControl = 0;
            countRowControl++;

            }
             }

        catch (Exception e){
            System.out.println("error");
        }


        jTable1.setModel(newTable2);
        jTable1.show();
        for(int i = 0; i < 4; i++){
            TableColumn column = jTable1.getColumnModel().getColumn(i);
            if (i==0) {
                column.setCellRenderer( centerRenderer );
            }
            if (i==1) {
                column.setCellRenderer( centerRenderer );
            }
            if (i==2) {
                column.setCellRenderer( centerRenderer );
            }
            if (i==3) {
                column.setPreferredWidth(40);
            }
    }
    }

    private void listAllBtnActionPerformed(java.awt.event.ActionEvent evt) {
    	firstPage.playAudio2(getClass().getResource("/resource/buttonHit.wav").getFile());
        initGetTable();
    }

    private void deleteBtnActionPerformed(java.awt.event.ActionEvent evt) {
        
        ListSelectionModel selectionModel = jTable1.getSelectionModel();
        
        
        String deleteQuery = null;
        
        if (jTable1.getSelectedRow() != -1){
        String eDate = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
        String eTime = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 1);
        String eType = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 2);
        String eTitle = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 3);
        String eDesc = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 4);
        
        
        deleteQuery = "DELETE FROM EVENT WHERE EVENT_DATE='"+eDate+"' AND EVENT_TIME='"+eTime+"' AND EVENT_TYPE='"+eType+"' AND EVENT_TITlE='"+eTitle+"' AND EVENT_DES='"+eDesc+"'";
        }

        String message = "Are you sure want to delete this event ?";
        String title2 = "Confirmation?";
        // display the JOptionPane showConfirmDialog
        int reply = JOptionPane.showConfirmDialog(null, message, title2, JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION)//delete from database once the user click "YES"
        {
            System.out.print(deleteQuery);
            updateRow(deleteQuery);
            JOptionPane.showMessageDialog(null, "Delete successful.");
            initGetTable();
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Delete aborted.");
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new listAllEvent().setVisible(true);
            }
        });
    }

    public void init()
    {
        groupButton();
        shaolinBtn.setVisible(false);
        jRadioButton1.setSelected(true);
        jComboBox1.setEnabled(false);
        sortBtn2.setEnabled(false);
        setDateBtn.setEnabled(true);
        sortBtn1.setEnabled(true);
        
    }

    private void groupButton() {

        ButtonGroup bg1 = new ButtonGroup( );
        bg1.add(jRadioButton1);
        bg1.add(jRadioButton2);
    }

    public void initGetTable()
    {
    	CenterRenderer centerRenderer = new CenterRenderer();
            NewTableModel newTable3 = new NewTableModel();
            int countRow = 0;
            int countColumn = 0;
            int countRowControl = 0;
            int countColumnControl = 0;

       try {

            ResultSet rset = userMainPage.stmt.executeQuery("SELECT EVENT_DATE,EVENT_TIME,EVENT_TYPE,EVENT_TITLE,EVENT_DES FROM EVENT WHERE USER_CODE="+userMainPage.userCode+" ORDER BY EVENT_DATE");



            //creating 2d arrays of appropriate size according to database
            while (rset.next()){
                countRow++;
            }

            ResultSetMetaData metadata = rset.getMetaData();
            countColumn = metadata.getColumnCount();

            newTable3.setDataSize(countRow, countColumn);
            newTable3.setColumnNameSize();
            //end

            //adding column names to table model
            DatabaseMetaData meta = userMainPage.con.getMetaData();

            ResultSet rset2 = meta.getColumns(null, null, "EVENT", null);


            newTable3.setColumnName(0, "DATE");
            newTable3.setColumnName(1, "TIME");
            newTable3.setColumnName(2, "TYPE");
            newTable3.setColumnName(3, "EVENT");
            newTable3.setColumnName(4, "DESCRIPTION");

            rset = userMainPage.stmt.executeQuery("SELECT EVENT_DATE,EVENT_TIME,EVENT_TYPE,EVENT_TITLE,EVENT_DES FROM EVENT WHERE USER_CODE="+userMainPage.userCode+" ORDER BY EVENT_DATE");
            //System.out.println(str);
            while(rset.next()){

            newTable3.addString(countRowControl, countColumnControl, rset.getString("EVENT_DATE"));
            countColumnControl++;
           
            newTable3.addString(countRowControl, countColumnControl, rset.getString("EVENT_TIME"));
            countColumnControl++;

            newTable3.addString(countRowControl, countColumnControl, rset.getString("EVENT_TYPE"));
            countColumnControl++;

            newTable3.addString(countRowControl, countColumnControl, rset.getString("EVENT_TITLE"));
            countColumnControl++;

            newTable3.addString(countRowControl, countColumnControl, rset.getString("EVENT_DES"));
            countColumnControl++;

            countColumnControl = 0;
            countRowControl++;

            }
             }

        catch (Exception e){
            System.out.println("error");
        }


        jTable1.setModel(newTable3);
        jTable1.show();
        for(int i = 0; i < 4; i++){
            TableColumn column = jTable1.getColumnModel().getColumn(i);
            if (i==0) {
                column.setCellRenderer( centerRenderer );
            }
            if (i==1) {
                column.setCellRenderer( centerRenderer );
            }
            if (i==2) {
                column.setCellRenderer( centerRenderer );
            }
            if (i==3) {
                column.setPreferredWidth(40);
            }
    }
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel Background;
    private javax.swing.JLabel backBtn;
    private javax.swing.JButton deleteBtn;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblSort;
    private javax.swing.JButton listAllBtn;
    public static javax.swing.JButton setDateBtn;
    private javax.swing.JLabel shaolinBtn;
    private javax.swing.JButton sortBtn1;
    private javax.swing.JButton sortBtn2;
    // End of variables declaration

}
